define(function(require, exports, module) {
    'use strict';

    var $ = require('jquery');

    var io = require('plugins/request');
    var box = require('plugins/box/box');
    require('plugins/serializeJSON');

    $('.jSendCode').click(function(){
        var self = this,
            nameVal = $.trim($('.jNameIpt').val()),
            reg = /1\d{10}/g;
        if((nameVal == '') || !reg.test(nameVal)){
            box.error('请填写正确的手机号！');
            return false;
        }
        if($(self).hasClass('disabled')){
            return false;
        }
        io.jsonp(window.location.href, function(){
            var second = 10;
            $(self).addClass('disabled');
            $(self).html('填写验证码（'+(--second)+'s）');
            var timer = setInterval(function(){
                second--;
                if(second == 0){
                    $(self).html('发送验证码').removeClass('disabled');
                    clearInterval(timer);
                }else{
                    $(self).html('填写验证码（'+second+'s）');
                }
            },1000);
        }, function(rst){
            box.error(rst.msg);
        });
    });

    $('.jSendIpt').keyup(function(){
        var flag = ($(this).val().length == 4);
        $('.jLogin')[flag?'removeClass':'addClass']('disabled');
    });

    $('.jLogin').click(function(){
        var self = this;
        if(!$(self).hasClass('disabled')){
            var data = $('#jLoginForm').serializeJSON();
            io.post('', function(){
                box.ok('登录成功！');
                var url = getParameter("returnUrl");
                window.location.href = url;
            }, function(rst){
                box.error(rst.msg);
            }, self);
        }
    });


    function getParameter(name){
         var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
         var r = window.location.search.substr(1).match(reg);
         if(r!=null)return  unescape(r[2]); return null;
    }

});
